<?php


	/*-------------------------------------*/
	/*---- PERSONNALISATION WP-ADMIN ------*/
	/*-------------------------------------*/
	
	// Changer le logo Authentification Wordpress
	function my_login_logo() {
		echo "<style type='text/css'>
		h1 a { background-image:url(".get_bloginfo('template_directory')."/images/logo.png) !important; }
		</style>";
	}
	add_action('login_head', 'my_login_logo');

	add_action( 'login_headerurl', 'my_login_logo_url' );
	function my_login_logo_url() {
		return $_SERVER['HTTP_HOST'];
	}

	add_filter( 'login_headertitle', 'my_login_title');
	function my_login_title(){
		return get_bloginfo('title', 'display' );
	}
	
	
	/*-------------------------------------*/
	/*---------- PROFIL MEMBRES -----------*/
	/*-------------------------------------*/
	
	function my_new_contactmethods( $contactmethods ) {
		
		/* Supprimer des champs */
		unset($contactmethods['aim']);
		unset($contactmethods['yim']);
		unset($contactmethods['jabber']);
		
		/* Ajouter des champs */
		$contactmethods['statut'] = 'Statut';
		$contactmethods['tel'] = 'Téléphone';
		$contactmethods['date_naissance'] = 'Date de naissance';
		$contactmethods['twitter'] = 'Twitter';
		$contactmethods['facebook'] = 'Facebook';
		$contactmethods['linkedin'] = 'LinkedIn';
		$contactmethods['viadeo'] = 'Viadeo';
		return $contactmethods;
	}
	add_filter('user_contactmethods','my_new_contactmethods',10,1);
	
	
	add_action( 'show_user_profile', 'extra_user_profile_fields' );
	add_action( 'edit_user_profile', 'extra_user_profile_fields' );

	function extra_user_profile_fields( $user ) { ?>
	<h3><?php _e("Informations professionnelles", "blank"); ?></h3>

	<table class="form-table">
	<tr>
	<th><label for="entreprise"><?php _e("Entreprise actuelle"); ?></label></th>
	<td>
	<input type="text" name="entreprise" id="entreprise" value="<?php echo esc_attr( get_the_author_meta( 'ent_actuelle', $user->ID ) ); ?>" class="regular-text" /><br />
	</td>
	</tr>
	<tr>
	<th><label for="description"><?php _e("Activité de l'entreprise"); ?></label></th>
	<td>
	<input type="text" name="description" id="description" value="<?php echo esc_attr( get_the_author_meta( 'desc_ent', $user->ID ) ); ?>" class="regular-text" />
	</td>
	</tr>
	<tr>
	<th><label for="poste"><?php _e("Poste"); ?></label></th>
	<td>
	<input type="text" name="poste" id="poste" value="<?php echo esc_attr( get_the_author_meta( 'poste', $user->ID ) ); ?>" class="regular-text" /><br />
	</td>
	</tr>
	<tr>
	<th><label for="mission"><?php _e("Mission"); ?></label></th>
	<td>
	<input type="text" name="mission" id="mission" value="<?php echo esc_attr( get_the_author_meta( 'mission', $user->ID ) ); ?>" class="regular-text" /><br />
	</td>
	</tr>
	</table>
	<?php }

	add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
	add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );

	function save_extra_user_profile_fields( $user_id ) {

	if ( !current_user_can( 'edit_user', $user_id ) ) { return false; }

	update_usermeta( $user_id, 'ent_actuelle', $_POST['entreprise'] );
	update_usermeta( $user_id, 'desc_ent', $_POST['description'] );
	update_usermeta( $user_id, 'poste', $_POST['poste'] );
	update_usermeta( $user_id, 'mission', $_POST['mission'] );
	}
	
	
	/*-------------------------------------*/
	/*---------- LOGIN / REGISTER ----------*/
	/*-------------------------------------*/
	
	/**
	* Permet de créer des pages /login, /profil et /logout
	**/

	add_action('send_headers','site_router');

	function site_router(){
	    $root = str_replace('index.php','',$_SERVER['SCRIPT_NAME']);
	    $url  = str_replace($root ,'',$_SERVER['REQUEST_URI']);
	    $url  = explode('/',$url);
	    if(count($url) == 1 && $url[0] == 'login'){
	        require 'page_connexion.php'; 
	        die();
	    }else if(count($url) == 1 && $url[0] == 'profil'){
			if (is_user_logged_in()){
				require 'profil.php'; 
		        die();
			}
			else {
				header('location:'.$root);
		        die();
			}
	    }else if(count($url) == 1 && $url[0] == 'logout'){
	        wp_logout();
	        header('location:'.$root);
	        die();
	    } else if(count($url) == 1 && $url[0] == 'register'){
	        require 'page_inscription.php'; 
	        die();
	    }
	}
	
	
	/*-------------------------------------*/
	/*---- JAVA SCRIPT --------------------*/
	/*-------------------------------------*/


	function my_scripts_method() {
		wp_deregister_script( 'jquery' );
		wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js');
		wp_enqueue_script( 'jquery' );
	}

	add_action('wp_enqueue_scripts', 'my_scripts_method');

	/*-------------------------------------*/
	/*---- THEME SUPPORT ------------------*/
	/*-------------------------------------*/

	add_theme_support("post-thumbnails");
	add_image_size( 'bloc', 310, 190, true);

	add_theme_support('nav-menus'); // je préviens wordpress que je veux utiliser les menus.
	register_nav_menus( array(   // enregistrement des menus
	'primary' => 'Menu principal',      // ici mon menu principal
	'footer' => 'Menu en pied de page',    // ici mon menu en pied de page
    ) );

	/*-------------------------------------*/
	/*---- SIDEBAR ------------------------*/
	/*-------------------------------------*/

	register_sidebar(array(
		'before_widget' => '<div class="sidepanel">',
		'after_widget' => '</div>',
		'before_title' => '<h3>',
		'after_title' => '</h3>',
	));

	register_sidebar( array(
		'name' => __( 'Footer Area One', 'twentyeleven' ),
		'id' => 'sidebar-3',
		'description' => __( 'An optional widget area for your site footer', 'twentyeleven' ),
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget' => "</aside>",
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );

	register_sidebar( array(
		'name' => __( 'Footer Area Two', 'twentyeleven' ),
		'id' => 'sidebar-4',
		'description' => __( 'An optional widget area for your site footer', 'twentyeleven' ),
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget' => "</aside>",
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );

	register_sidebar( array(
		'name' => __( 'Footer Area Three', 'twentyeleven' ),
		'id' => 'sidebar-5',
		'description' => __( 'An optional widget area for your site footer', 'twentyeleven' ),
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget' => "</aside>",
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );

	register_sidebar( array(
			'name' => __( 'Index Area One', 'alumni' ),
			'id' => 'sidebar-index-1',
			'description' => __( 'An widget area for your site index', 'twentyeleven' ),
			'before_widget' => '<div id="%1$s" class="widget %2$s">',
			'after_widget' => "</div>",
			'before_title' => '<h3 class="widget-title">',
			'after_title' => '</h3>',
		) );

		register_sidebar( array(
			'name' => __( 'Index Area Two', 'alumni' ),
			'id' => 'sidebar-index-2',
			'description' => __( 'An widget area for your site index', 'twentyeleven' ),
			'before_widget' => '<div id="%1$s" class="widget %2$s">',
			'after_widget' => "</div>",
			'before_title' => '<h3 class="widget-title">',
			'after_title' => '</h3>',
		) );

		register_sidebar( array(
			'name' => __( 'Index Area Three', 'alumni' ),
			'id' => 'sidebar-index-3',
			'description' => __( 'An widget area for your site index', 'alumni' ),
			'before_widget' => '<div id="%1$s" class="widget %2$s">',
			'after_widget' => "</div>",
			'before_title' => '<h3 class="widget-title">',
			'after_title' => '</h3>',
		) );


	/*-------------------------------------*/
	/*---- FUNCTION -----------------------*/
	/*-------------------------------------*/

	/**
	 * Count the number of footer sidebars to enable dynamic classes for the footer
	 */
	function footer_sidebar_class() {
		$count = 0;
		if ( is_active_sidebar( 'sidebar-3' ) )
			$count++;
		if ( is_active_sidebar( 'sidebar-4' ) )
			$count++;
		if ( is_active_sidebar( 'sidebar-5' ) )
			$count++;
		$class = '';
		switch ( $count ) {
			case '1':
				$class = 'one';
				break;
			case '2':
				$class = 'two';
				break;
			case '3':
				$class = 'three';
				break;
		}

		if ( $class )
			echo 'class="' . $class . '"';
	}

	function theme_navigation () {
		if(function_exists('wp_pagenavi')) :
			wp_pagenavi();
		else :

			echo('<div class="navleft">'.get_next_posts_link('Anciens articles', '0').'</div>
			<div class="navright">'.get_previous_posts_link('Nouveaux articles', '0').'</div>');
		endif;
	}

	function theme_meta () {

		echo('<span>Post&eacute; par '.get_the_author().' le '.get_the_time('j F Y').' dans : </span>');
		the_category(', ');
		echo (' <span class="sep">|</span> ');
		comments_popup_link('Poster un Commentaire', '1 Comment', '% Comments');
	}

	function theme_content () {
		the_content('Click here to read more &raquo;');
	}

	function get_annuaire() {
		$query = "SELECT * FROM membres;";
		$res = mysql_query($query) or die ("erreur de requ�te :".$query);
		return $res;
	}

	function affichage_annuaire() {
		mysql_connect("localhost", "root", "") or die ();
		mysql_select_db("prag") or die ("erreur : " ."prag");
		$liste = get_annuaire();
		while($data = mysql_fetch_assoc($liste)){
			$id = $data['Id_membre'];
			$nom = $data['Nom_membre'];
			$prenom = $data['Prenom_membre'];
			$tel = $data['tel_membre'];
			$mail = $data['adrM_membre'];
			echo "<div> Nom : $nom Pr&eacute;nom :  $prenom  Mail : $mail T&eacute;l&eacute;phone :  $tel </div>";
		}
	}

	function get_nom_profil($userid) {
		$nom = get_metadata('user', $userid, 'first_name', true);
		$prenom = get_metadata('user', $userid, 'last_name', true);
		return $nom . " " . $prenom;
	}

	function get_actu_profil($userid) {
		$query = "SELECT * FROM wp_posts WHERE id IN (SELECT post_id FROM wp_assoc_actu_user WHERE user_id=$userid) ORDER BY post_date DESC;";
		$res = mysql_query($query) or die ("erreur de requ�te :".$query);
		return $res;
	}

	function affichage_actu_profil($userid){
		mysql_connect("localhost", "root", "") or die ();
		mysql_select_db("prag") or die ("erreur : " ."prag");
		$listeActu = get_actu_profil($userid);
		while($data = mysql_fetch_assoc($listeActu)){
			$titre = $data['post_title'];
			$date = $data['post_date'];
			$contenu = $data['post_content'];
			$id = $data['ID'];
			wp_trim_excerpt($contenu);
			//echo "<div> Titre :".$titre." Date :".$date." Contenu :".$contenu."</div>";
			echo "<a href='http://prag/?p=$id'><div> Titre :$titre Date :$date Contenu : </div></a>";
		}
	}

	function get_donnnee($donnee){
		return get_metadata('user', get_current_user_id(), $donnee, true);
	}


?>